/*******************************************************************************
 * Main View                                       http://binground.codeplex.com
 *                                              Microsoft Public License (Ms-PL)
 * Authors
 *  - Michael Flanakin  2008-09  http://www.michaelflanakin.com
 *
 * page
 *  - init : initializes the page
 *  - load : loads the page
 ******************************************************************************/
var page =
{
	//***| private |************************************************************

	container: null,
	bgimg: null,
	timestamp: null,
	updateIcon: null,
	openIcon: null,


	//***| public |*************************************************************

	showFullImage: function() {
		var options = {};
		/*
		 * This code shrinks the gadget before showing the flyout when the screen resolution is too small for the large 
		 * size and the flyout size. Unfortunately, it keeps it's top-left origin, which causes smaller resolutions to 
		 * show the flyout on the right, whereas the intent was to keep the top-right origin, and show the flyout on the 
		 * left. The only way this seems to be possible is by having the user shrink the gadget manually. I'm leaving 
		 * this code in, but it does not fully work as intended.
		 *
		 * Consider limiting the size to one that can host both the gadget and its flyout. This may not be possible 
		 * until a custom size is possible via settings, where we can easily warn the user that the flyout will be 
		 * displayed incorrectly for certain sizes.
		 */
		if ($.gadget.undocked() && screen.availWidth < (page.bgimg.width() + 1024)) {
			options.show = function(){ page.resize(true); };
			options.hide = function(){ page.resize(); };
		}
		$.gadget.flyout(binground.pages.flyout, options);
	},

	load: function() {
		// only update on refresh intervals
		var timeSinceLastUpdate = (page.timestamp[0].date == null) ? binground.refreshInterval + 1 : (new Date().getTime() - page.timestamp[0].date.getTime());
		if (timeSinceLastUpdate >= binground.refreshInterval) {
			// show image
			var img = binground.getImage();

			// if not empty or new, update image/time
			if (!$.string.isEmpty(img) || page.bgimg[0].src == img) {
				page.bgimg[0].src = img;
				page.container.css({ display: "" });
				page.timestamp[0].date = new Date();
			}
		}

		// reset timestamp display to remove visibility change flag
		page.timestamp.text($.date.toString(page.timestamp[0].date));
		page.timestamp.attr({ title: "Updated on " + $.date.toString(page.timestamp[0].date) + " at " + $.date.toTimeString(page.timestamp[0].date) });

		// reload every minute to lessen wakeup time after sleep
		setTimeout("page.load();", binground.refreshInterval);
	},

	checkForUpdate: function() {
		if (binground.isUpdateAvailable()) { page.updateIcon.css({ display: "inline" }); return; }
		setTimeout("page.checkForUpdate();", binground.updateCheckInterval);
	},

	init: function() {
		// load controls
		page.body = $("#main");
		page.container = $("#container");
		page.bgimg = $("#bgimg");
		page.timestamp = $("#timestamp");
		page.updateIcon = $("#updateIcon");
		page.openIcon = $("#openIcon");

		// add control event handlers
		page.body.addClass("js");
		page.bgimg.dblclick(page.showFullImage);
		$(".dim").mouseenter(function() { util.opacity.darkenMore(this); }).mouseleave(function() { util.opacity.lightenLess(this); });
		page.openIcon.click(page.showFullImage);

		// validate that all controls have loaded successfully
		if (page.container.length == 0 || page.bgimg.length == 0 || page.timestamp.length == 0) { alert("Not all page controls loaded!"); return; }

		// attach focus/blur and dock/undock handlers
		$.gadget
			.focus(page.visibilityChanged)
			.blur(page.visibilityChanged)
			.dock(page.resize)
			.undock(page.resize);
		
		// initial sizing and load
		page.resize();
		page.load();

		// check for update
		page.checkForUpdate(); //setTimeout("page.checkForUpdate();", 5*60*1000); // delay for 5 seconds -- for some reason, validation check doesn't come back the first time
	},
	
	resize: function(force){
		var size = ($.gadget.docked() || force) ? binground.dockedSize : binground.undockedSize;
		page.body.css({ height: size.height, width: size.width });
		if ($.gadget.docked() || force) { page.body.removeClass("undocked"); } else { page.body.addClass("undocked"); }
	},

	visibilityChanged: function() {
		page.load();
		page.timestamp.text(page.timestamp.text() + ($.gadget.browser() ? " *" : " *"));
		if ($.gadget.sidebar()) {
			if ($.string.endsWith(page.timestamp[0].title, " (" + !$.gadget.visible() + ")")) {
				page.timestamp[0].title = page.timestamp[0].title.replace(" (" + !$.gadget.visible() + ")", " (" + $.gadget.visible() + ")");
			}
			else {
				page.timestamp[0].title += " (" + $.gadget.visible() + ")";
			}
		}
	}
};
